package com.deer.wms.busine.tactic.web;

import com.deer.wms.busine.tactic.model.Batch.*;
import com.deer.wms.busine.tactic.service.BatchTacticDetailDetailService;
import com.deer.wms.busine.tactic.service.BatchTacticDetailService;
import com.deer.wms.busine.tactic.service.BatchTacticService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2019/11/29.
*/
@Api(description = "批次信息api接口")
@RestController
@RequestMapping("/batch/tactics")
public class BatchTacticController {

    @Autowired
    private BatchTacticService batchTacticService;

    @Autowired
    private BatchTacticDetailService batchTacticDetailService;

    @Autowired
    private BatchTacticDetailDetailService batchTacticDetailDetailService;

    @Autowired
    private RedisUtil redisUtil;


    @ApiOperation(value = "添加批次属性信息", notes = "添加批次属性信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/insert")
    public Result add(@RequestBody InsertData insertData,@ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        batchTacticService.createBatchTactic(insertData,currentUser);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {

//        BatchTactic batchTactic = batchTacticService.getById(id);
//        batchTacticDetailService.deleteByCode(batchTactic.getBatchTacticCode());
        batchTacticService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "更新批次属性信息", notes = "更新批次属性信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/update")
    public Result update(@RequestBody  InsertData insertData,@ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        batchTacticService.updateBatchTactic(insertData,currentUser);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        BatchTactic batchTactic = batchTacticService.getById(id);
        return ResultGenerator.genSuccessResult(batchTactic);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @GetMapping("/list")
    public Result list(BatchTacticCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<BatchTactic> list = batchTacticService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @ApiOperation(value = "根据状态查list", notes = "根据状态查list")
    @GetMapping("/StateList")
    public Result findByState(Integer batchTacticId,Integer state, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<BatchTacticDto> list = batchTacticService.findByState(batchTacticId,state);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @ApiOperation(value = "根据货主或物料查list", notes = "根据货主或物料查list")
    @GetMapping("/findByOrganizationOrItemInfo")
    public Result findByOrganizationOrItemInfo(Integer organizationId,String itemCode, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<BatchTacticDto> list = batchTacticService.findByOrganizationOrItemInfo(organizationId,itemCode);
        if(list ==null){
            return ResultGenerator.genFailResult(CommonCode.BATCH_TACTIC_ERROR, null);
        }
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @ApiOperation(value = "根据货主或物料查list", notes = "根据货主或物料查list")
    @GetMapping("/findByOrganizationOrItemInfo2")
    public Result findByOrganizationOrItemInfo2(Integer organizationId,String itemCode, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<BatchTacticDto> list = batchTacticService.findByOrganizationOrItemInfo2(organizationId,itemCode);
        if(list ==null){
            return ResultGenerator.genFailResult(CommonCode.BATCH_TACTIC_ERROR, null);
        }
        return ResultGenerator.genSuccessResult(list);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @ApiOperation(value = "根据BatchTacticCode查list", notes = "根据BatchTacticCode查list")
    @GetMapping("/findByBatchTacticCode")
    public Result findByBatchTacticCode(String batchTacticCode, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        if(batchTacticCode==null ||batchTacticCode.trim().equals("") ){
            batchTacticCode = "sys_batch_tactic";
        }
        BatchTacticDto list =(BatchTacticDto)redisUtil.get(batchTacticCode+"batchTactic");

        if(list ==null){
            list = batchTacticService.findByBatchTacticCode(batchTacticCode);
            redisUtil.set(batchTacticCode+"batchTactic",list);
        }
        if(list ==null){
            list = batchTacticService.findByBatchTacticCode("sys_batch_tactic");
            redisUtil.set( "sys_batch_tacticbatchTactic",list);
        }

        if(list==null){
            return ResultGenerator.genFailResult(CommonCode.BATCH_TACTIC_ERROR, null);
        }
        return ResultGenerator.genSuccessResult(list);
    }
}
